package com.mandala.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.ApplicationSystem;
import com.mandala.entry.Group;
import com.mandala.entry.Org;
import com.mandala.entry.Permission;
import com.mandala.entry.ResponseWrapper;
import com.mandala.entry.Role;
import com.mandala.entry.Synthesis;
import com.mandala.entry.User;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api("用户查询接口")
public class UserController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	
	@Autowired
	private LDAPConnectService connectService;
	
	@Autowired
	private JsonResult jsonResult;
	
	
	static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
	
	
    @ApiOperation(value = "根据用户ID", notes = "用户查询")
    @ApiImplicitParam(name = "cn", value = "用户ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getUserByCn/{cn}" , method = RequestMethod.GET)
    @ResponseBody
    public  ResponseWrapper getUserByCn(@PathVariable("cn") String cn) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(cn.equals("null") || cn ==null){
    		return jsonResult.markParamError();
    	}
        User user = new User();
        user = odmUserRepo.findUserByCn(cn);
		if(user != null){
			response = jsonResult.markSuccess(user);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    
    

	@ApiOperation(value = "根据用户工号", notes = "用户查询")
    @ApiImplicitParam(name = "uid", value = "用户工号", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getUserByUid/{uid}" , method = RequestMethod.GET)
    public  ResponseWrapper getUserByUid(@PathVariable("uid") String uid) {
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	ResponseWrapper response = new ResponseWrapper();
    	if(uid.equals("null") || uid ==null){
    		return jsonResult.markParamError();
    	}
        User user = new User();
        user = odmUserRepo.findUserByUid(uid);
        if(user != null){
			response = jsonResult.markSuccess(user);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    @ApiOperation(value = "根据用户工号", notes = "用户系统查询")
    @ApiImplicitParam(name = "uid", value = "用户工号", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getUserSystemListByUid/{uid}" , method = RequestMethod.GET)
    public  ResponseWrapper getUserSystemListByUid(@PathVariable("uid") String uid) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(uid.equals("null") || uid ==null){
    		return jsonResult.markParamError();
    	}
    	User user = odmUserRepo.findUserByUid(uid);
    	if(user == null){
			return jsonResult.markSuccessButNoData();
        }
        String cn = user.getCn();
        //通过用户的id来查询用户绑定的业务系统
        List<ApplicationSystem> listSystem = odmUserRepo.findAllSystemByUserId(cn);
        if(listSystem.size()>0){
			response = jsonResult.markSuccess(listSystem);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    @ApiOperation(value = "根据用户工号和系统ID", notes = "用户系统权限查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "uid", value = "用户工号", paramType = "path", required = true, dataType = "String"),
    	@ApiImplicitParam(name = "systemId", value = "系统ID", paramType = "path", required = true, dataType = "String")})
    @RequestMapping(value = "/getUserSystemListByUid/{uid}/{systemId}" , method = RequestMethod.GET)
    public  ResponseWrapper getUserSystemPermissionByUidAndSystemId(@PathVariable("uid") String uid,@PathVariable("systemId") String systemId) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(uid.equals("null") || uid ==null){
    		return jsonResult.markParamError();
    	}
    	if(systemId.equals("null") || systemId ==null){
    		return jsonResult.markParamError();
    	}
    	User user = odmUserRepo.findUserByUid(uid);
    	if(user == null){
			return jsonResult.markSuccessButNoData();
        }
        
        List<Permission> listPermission = odmUserRepo.findPermissionByConditions(user,systemId);
        if(listPermission.size()>0){
			response = jsonResult.markSuccess(listPermission);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        
        return response;
    }
    
	@ApiOperation(value = "根据用户工号", notes = "用户所有关联查询")
    @ApiImplicitParam(name = "uid", value = "用户工号", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getAllByUid/{uid}" , method = RequestMethod.GET)
    public  ResponseWrapper getAllByUid(@PathVariable("uid") String uid) {
		ResponseWrapper response = new ResponseWrapper();
		if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(uid.equals("null") || uid ==null){
    		return jsonResult.markParamError();
    	}
    	Synthesis synthesis = new Synthesis();
    	User user = odmUserRepo.findUserByUid(uid);
    	if(user == null){
			return jsonResult.markSuccessButNoData();
        }
        synthesis.setUser(user);
        List<String> ouList = user.getOu();
        List<Org> listOrg = new ArrayList<Org>();
        //通过用户ou,来查询用户绑定的组织机构
        if(ouList.size()>0){
        	for(int i = 0;i<ouList.size();i++){
        		Org org = new Org();
        		String ou = ouList.get(i);
        		org = odmUserRepo.findOrgByOu(ou);
        		if(org != null){
        			listOrg.add(org);
        		}
        		
        	}
        }
        synthesis.setOrg(listOrg);
        String cn = user.getCn();
        //通过用户的id来查询用户绑定的群组
        List<Group> listGroup = odmUserRepo.findAllGroupByUserId(cn);
        synthesis.setGroup(listGroup);
        //通过用户的工号来查询用户绑定的角色
        List<Role> listRole = odmUserRepo.findRoleByConditions(user);
        synthesis.setRole(listRole);
        //通过用户的id来查询用户绑定的业务系统
        List<ApplicationSystem> listSystem = odmUserRepo.findAllSystemByUserId(cn);
        synthesis.setSystem(listSystem);
        if(synthesis != null){
			response = jsonResult.markSuccess(synthesis);
        }
        return response;
    }
    
    /*@ApiOperation(value="获取用户列表", notes="获取所有用户列表")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public List<User> getUserList() {
    	List<User> r = odmUserRepo.findAllUser();
        return r;
         
     }*/
}